import { configureStore, combineReducers } from "@reduxjs/toolkit";
import { useDispatch, useSelector } from "react-redux";
// 切片
import DemoSlice from "./sliceReducer/demo";
import HomeSlice from "./sliceReducer/HomeSliceReducer";
import UserSlice from "./sliceReducer/UserSliceReudcer";
import PatientCircleSlice from "./sliceReducer/PatientCircleSliceReducer";
// 配置持久化
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage"; // defaults to localStorage for web
// 导入类型
import type { RootState } from "../type/store";

const persistConfig = {
  key: "root",
  storage,
  whitelist: ['home','User','patientCircle'],
  blackList:['demo']
};  

// 切片合并
const combine = combineReducers({
  demo: DemoSlice,
  home: HomeSlice,
  user: UserSlice,
  patientCircle: PatientCircleSlice,
});

// 切片持久化
const persistedReducer = persistReducer(persistConfig, combine);

// 创建store对象
const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: {
        ignoredActions: ['persist/PERSIST', 'persist/REHYDRATE'],
      },
    }),
});

export const persistor = persistStore(store);

export type AppDispatch = typeof store.dispatch;
export const useAppDispatch = useDispatch.withTypes<AppDispatch>();
export const useAppSelector = useSelector.withTypes<RootState>();
export default store;
